/*
 * WindowListenerAdapter2.cpp
 *
 *  Created on: 2012-8-28
 *      Author: yangentao@gmail.com
 */

#include <windows.h>
#include <windowsx.h>
#include "window.h"
#include "WindowListenerAdapter.h"

#include "../win32/win32.h"

namespace ui {

//all message except onCreating/HCBT_CREATEWND can process by onMessage
//include onCreated/onDestroy/onNcDestroy/onKeydown/onKeyUp/onCommand, etc
HRESULT WindowListenerAdapter::onMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& handled) {
	switch (uMsg) {
	case WM_CREATE: {
		HRESULT hr = 0;
		if (OnCreated) {
			if (!OnCreated(hWnd, (CREATESTRUCT*) lParam)) {
				handled = TRUE;
				hr = -1;
			}
		}
		if (OnCreateChildren) {
			OnCreateChildren();
		}
		return hr;
	}
		break;
	case WM_ACTIVATE:
		if (OnActivate) {
			WORD lo = LOWORD(wParam);
			WORD hi = HIWORD(wParam);
			OnActivate(lo == WA_ACTIVE || lo == WA_CLICKACTIVE, lo == WA_CLICKACTIVE, hi);
		}
		break;
	case WM_CLOSE:
		if (OnClose) {
			if (!OnClose()) {
				handled = TRUE;
				return 0;
			}
		}
		break;
	case WM_DESTROY:
		if (OnDestroy) {
			OnDestroy();
		}
		break;
	case WM_NCDESTROY:
		if (OnNcDestroy) {
			OnNcDestroy();
		}
		break;
	case WM_SYSKEYDOWN: //fallthrough
	case WM_KEYDOWN:
		if (OnKeyDown) {
			OnKeyDown(isKeyDown(VK_MENU), isKeyDown(VK_CONTROL), isKeyDown(VK_SHIFT), wParam, lParam, (lParam & 0x40000000), handled);
			if (handled) {
				return 0;
			}
		}
		break;
	case WM_SYSKEYUP: //fallthrough
	case WM_KEYUP:
		if (OnKeyUp) {
			OnKeyUp(isKeyDown(VK_MENU), isKeyDown(VK_CONTROL), isKeyDown(VK_SHIFT), wParam, lParam, handled);
			if (handled) {
				return 0;
			}
		}
		break;

	case WM_SIZE:
		if (OnSize) {
			OnSize(wParam, LOWORD(lParam), HIWORD(lParam) );
		}
		if (OnLayoutChildren && wParam != SIZE_MINIMIZED) {
			OnLayoutChildren();
		}
		break;
	case WM_COMMAND: {
		WORD hi = HIWORD(wParam);
		WORD lo = LOWORD(wParam);
		if (hi == 0) {
			if (OnMenuItem) {
				OnMenuItem(lo);
			}
		} else if (hi == 1) {
			if (OnAccelerator) {
				OnAccelerator(lo);
			}
		} else {
			if (OnControl) {
				OnControl(hi, lo, (HWND) lParam);
			}
		}
		break;
	}
	case WM_MENUCOMMAND:
		if (OnMenuItemByPos) {
			OnMenuItemByPos((HMENU) lParam, wParam);
		}
		break;
	case WM_SYSCOMMAND:
		wParam = wParam & 0XFFF0;
		switch (wParam) {
		case SC_CLOSE:
			if (OnSystemClose) {
				OnSystemClose();
			}
			break;
		case SC_CONTEXTHELP:
			if (OnSystemContextHelp) {
				OnSystemContextHelp();
			}
			break;
		case SC_MOUSEMENU:
			if (OnSystemMenu) {
				int x = GET_X_LPARAM(lParam);
				int y = GET_Y_LPARAM(lParam);
				OnSystemMenu(true, x, y, 0);
			}
			break;
		case SC_KEYMENU:
			if (OnSystemMenu) {
				DWORD ch = wParam;
				OnSystemMenu(false, 0, 0, ch);
			}
			break;
		case SC_MAXIMIZE:
			if (OnSystemMaximize) {
				OnSystemMaximize();
			}
			break;
		case SC_MINIMIZE:
			if (OnSystemMinimize) {
				OnSystemMinimize();
			}
			break;
		case SC_HSCROLL:
			if (OnSystemHScroll) {
				OnSystemHScroll();
			}
			break;
		case SC_VSCROLL:
			if (OnSystemVScroll) {
				OnSystemVScroll();
			}
			break;
		case SC_SIZE:
			if (OnSystemSizing) {
				OnSystemSizing();
			}
			break;
		case SC_TASKLIST:
			if (OnSystemTaskList) {
				OnSystemTaskList();
			}
			break;
		case SC_RESTORE:
			if (OnSystemRestore) {
				OnSystemRestore();
			}
			break;
		case SC_MOVE:
			if (OnSystemMove) {
				OnSystemMove();
			}
			break;
		case SC_PREVWINDOW:
			if (OnSystemMovePre) {
				OnSystemMovePre();
			}
			break;
		case SC_NEXTWINDOW:
			if (OnSystemMoveNext) {
				OnSystemMoveNext();
			}
			break;
		case SC_DEFAULT:
			if (OnSystemDefault) {
				OnSystemDefault();
			}
			break;
		case SC_SCREENSAVE:
			if (OnSystemScreenSave) {
				OnSystemScreenSave();
			}
			break;
		case SC_MONITORPOWER:
			if (OnSystemMonitorPower) {
				OnSystemMonitorPower(lParam == 1, lParam == 2);
			}
			break;
		case SC_HOTKEY:
			if (OnSystemHotKey) {
				OnSystemHotKey((HWND) lParam);
			}
			break;

		}
		break;
	}
	return 0;
}

} /* namespace ui */
